package com.atguigu.tingshu.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/***
 * 订单延迟消息的配置
 */
@Configuration
public class OrderDelayRabbitConfig {

    /**
     * 普通交换机
     */
    @Bean("tingshuOrderExchange")
    public Exchange tingshuOrderExchange(){
        return ExchangeBuilder.directExchange("tingshu_order_exchange").build();
    }

    /**
     * 普通队列 懒汉模式
     * @return
     */
    @Bean("tingshuOrderQueue")
    public Queue tingshuOrderQueue(){
        return QueueBuilder
                .durable("tingshu_order_queue")
                .withArgument("x-dead-letter-exchange", "tingshu_order_exchange") // 将死信消息要交给的交换机(原交换机)
                .withArgument("x-dead-letter-routing-key", "tingshu.order.dead") // 死信消息的routingKey
                .build();
    }

    /**
     * 死信队列 饿汉模式
     * @return
     */
    @Bean("tingshuOrderDeadQueue")
    public Queue tingshuOrderDeadQueue(){
        return QueueBuilder.durable("tingshu_order_dead_queue").build();
    }

    /**
     * 普通队列绑定
     * @param tingshuOrderExchange
     * @param tingshuOrderQueue
     * @return
     */
    @Bean
    public Binding tingshuOrderQueueBinding(@Qualifier("tingshuOrderExchange") Exchange tingshuOrderExchange,
                                            @Qualifier("tingshuOrderQueue") Queue tingshuOrderQueue){
        return BindingBuilder.bind(tingshuOrderQueue).to(tingshuOrderExchange).with("tingshu.order").noargs();
    }

    /**
     * 死信队列绑定
     * @param tingshuOrderExchange
     * @param tingshuOrderDeadQueue
     * @return
     */
    @Bean
    public Binding tingshuOrderDeadQueueBinding(@Qualifier("tingshuOrderExchange") Exchange tingshuOrderExchange,
                                            @Qualifier("tingshuOrderDeadQueue") Queue tingshuOrderDeadQueue){
        return BindingBuilder.bind(tingshuOrderDeadQueue).to(tingshuOrderExchange).with("tingshu.order.dead").noargs();
    }


}
